package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.GoodsFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.dao.MyCollectionMapper;
import com.changgou.order.pojo.MyCollection;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
	private static final String CART="Cart_";
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private GoodsFeign goodsFeign;
	@Autowired
	private MyCollectionMapper myCollectionMapper;
	@Override
	public void addCart(String skuId, Integer num,String username){
		/**
		 * 1）查询redis中的数据
		 * 2）如果redis中已经有了，则追加数量，重新计算金额
		 * 3）如果没有，将商品添加到缓存
		 */
//		Sku sku = goodsFeign.findById(skuId).getData();
//		Spu spu = goodsFeign.findSpuById(sku.getSpuId()).getData();
//		OrderItem orderItem = this.sku2OrderItem(sku, spu, num);
//		System.out.println(orderItem);
		OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART+username).get(skuId);
		if (orderItem != null){
			//存在，刷新购物车
			orderItem.setNum(orderItem.getNum()+num);
			if(orderItem.getNum()<=0){
				orderItem.setNum(1);
			}
			orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
			orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());
		}else{
//			//不存在，新增购物车
			Sku sku = goodsFeign.findById(skuId).getData();
			Spu spu = goodsFeign.findSpuById(sku.getSpuId()).getData();
			orderItem = this.sku2OrderItem(sku, spu, num);
		}
		//存入redis
		redisTemplate.boundHashOps(CART+username).put(skuId,orderItem);
	}

	@Override
	public Map list(String username) {
		Map map = new HashMap();
		List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART+username).values();
		map.put("orderItemList",orderItemList);
		//商品数量与总价格
		Integer totalNum = 0;
		Integer totalPrice = 0;
		for (OrderItem orderItem : orderItemList) {
			totalNum +=orderItem.getNum();
			totalPrice+=orderItem.getMoney();
		}
		map.put("totalNum",totalNum);
		map.put("totalPrice",totalPrice);
		return map;
	}

	/**
	 * 添加收藏
	 * @param myCollection
	 */
	@Override
	public void addCol(MyCollection myCollection) {

		myCollectionMapper.insert(myCollection);
	}

	//sku转换为orderItem
	private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
		OrderItem orderItem = new OrderItem();
		orderItem.setSpuId(sku.getSpuId());
		orderItem.setSkuId(sku.getId());
		orderItem.setName(sku.getName());
		orderItem.setPrice(sku.getPrice());
		orderItem.setNum(num);
		orderItem.setMoney(num*orderItem.getPrice());       //单价*数量
		orderItem.setPayMoney(num*orderItem.getPrice());    //实付金额
		orderItem.setImage(sku.getImage());
		orderItem.setWeight(sku.getWeight()*num);           //重量=单个重量*数量
		//分类ID设置
		orderItem.setCategoryId1(spu.getCategory1Id());
		orderItem.setCategoryId2(spu.getCategory2Id());
		orderItem.setCategoryId3(spu.getCategory3Id());
		return orderItem;
	}
}
